﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.WebPartPages;
using System.Xml;

namespace PepsiCo.RBS.WebParts.CalendarFilter
{
    public partial class CalendarFilterUserControl : UserControl
    {
        #region Private variable
        private ListViewWebPart listViewWebPart;
        private XsltListViewWebPart xsltListViewWebPart;
        #endregion

        protected ListViewWebPart FindListViewWebPart(Control control)
        {
            ListViewWebPart listview = null;
            if (control is ListViewWebPart)
            {
                listview = control as ListViewWebPart;
            }
            else
            {
                foreach (Control child in control.Controls)
                {
                    listview = FindListViewWebPart(child);
                    if (listview != null)
                    {
                        break;
                    }
                }
            }
            return listview;
        }

        protected XsltListViewWebPart FindXsltListViewWebPart(Control control)
        {
            XsltListViewWebPart listview = null;
            if (control is XsltListViewWebPart)
            {
                listview = control as XsltListViewWebPart;
            }
            else
            {
                foreach (Control child in control.Controls)
                {
                    listview = FindXsltListViewWebPart(child);
                    if (listview != null)
                    {
                        break;
                    }
                }
            }
            return listview;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetQueryListViewWebPart();
                SetQueryXsltListViewWebPart();
            }
        }

        private void SetQueryListViewWebPart()
        {
            listViewWebPart = FindListViewWebPart(Page);

            if (xsltListViewWebPart != null)
            {
                string abc = "";
            }
        }

        private void SetQueryXsltListViewWebPart()
        {
            string query = "";
            xsltListViewWebPart = FindXsltListViewWebPart(Page);

            if (xsltListViewWebPart != null)
            {
                XmlDocument xml = new XmlDocument();
                xml.LoadXml(xsltListViewWebPart.XmlDefinition);

                XmlElement viewXml = xml["View"];
                XmlNode viewQuery = viewXml.SelectSingleNode("//Query");
                XmlNode where = viewQuery.SelectSingleNode("//Where");

                if (where == null)
                {
                    where = xml.CreateElement("Where");
                    viewQuery.AppendChild(where);
                }

                if (where.ChildNodes.Count == 1)
                {
                    where.InnerXml = string.Format("<And>{0}{1}</And>", query, where.FirstChild.OuterXml);
                }
                else
                {
                    where.InnerXml = query;
                }
                xsltListViewWebPart.XmlDefinition = xml.InnerXml;
            }
        }

        
    }
}
